package bud.mobile.controller;


import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import bud.mobile.service.IMobileMemberService;
import bud.mobile.service.IMobileMemberService.MemberResult;
import bud.vo.FriendDTO;
import bud.vo.MemberDTO;

@Controller
@Slf4j
public class BudController {
	@Inject
	private IMobileMemberService service;
	public IMobileMemberService getService() {
		return service;
	}
	public void setService(IMobileMemberService service) {
		this.service = service;
	}
	
	@RequestMapping("bud/budList")
	public String budList(){
		return "bud/budList";
	}
	
	@RequestMapping("bud/budMyProfile")
	public String budMyProfile(String msg, Model model, HttpServletRequest request){
		HttpSession session = request.getSession();
		MemberDTO member = (MemberDTO)session.getAttribute("memberSession");
		model.addAttribute("commentMemberList",service.getCommentMember(member.getMem_no()));
		if ("ok".equals(msg)){
			model.addAttribute("msg", "수정완료~");
		}
		return "bud/budMyProfile";
	}
	
	@RequestMapping("bud/budProfile")
	public String budProfile(int mem_no, Model model){
		MemberDTO member = service.getMember(mem_no);
		model.addAttribute("member", member);
		model.addAttribute("commentMemberList",service.getCommentMember(mem_no));
		return "bud/budProfile";
	}
	
	@RequestMapping("bud/budMyProfileEdit")
	public String budMyProfileEdit(MemberDTO member, MultipartHttpServletRequest request){
		MemberResult result = service.updateMember(member);
		MemberDTO memberEdit = service.getMember(member.getMem_no());
		HttpSession session = request.getSession();		// session 내장객체를 가져온다.
		session.setAttribute("memberSession", memberEdit);
		return "redirect:/bud/budMyProfile.mo?msg="+"ok";
	}
	
	@RequestMapping("bud/budSearch")
	public String budSearch(){
		return "bud/budSearch";
	}
	
	@RequestMapping("bud/budSearchAction")
	public String budSearchAction(String mem_email, Model model){
		MemberDTO searchBud = service.getMemberByEmail(mem_email);
		if (searchBud != null){
			model.addAttribute("member", searchBud);
		}
		return "bud/budSearch";
	}
	
	@RequestMapping("bud/budReceiveRequest")
	public String budReceiveRequest(Model model, String already, HttpServletRequest request){
		HttpSession session = request.getSession();
		MemberDTO member = (MemberDTO)session.getAttribute("memberSession");
		if ("true".equals(already)){
			model.addAttribute("msg", "이미 상대방이 벗 신청을 하였습니다!!");
		}

		List<FriendDTO> friendList = (List<FriendDTO>)service.getRequestFriendList(member.getMem_no());
		model.addAttribute("friendList", friendList);
		return "bud/budReceiveRequest";
	}
	
	@RequestMapping("bud/budReceiveRequestBud")
	public String budReceiveRequestBud(int mem_no, Model model, HttpServletRequest request){
		HttpSession session = request.getSession();
		MemberDTO member = (MemberDTO)session.getAttribute("memberSession");
		
		FriendDTO friend = new FriendDTO();
		friend.setFr_member(member.getMem_no());
		friend.setFr_friend(mem_no);
		friend.setFr_request("y");
		service.updateFriend(friend);

		friend.setFr_member(mem_no);
		friend.setFr_friend(member.getMem_no());
		service.insertFriend(friend);
		
		member = service.getMember(member.getMem_no());
		session.setAttribute("memberSession", member);
		
//		List<FriendDTO> friendList = (List<FriendDTO>)service.getRequestFriendList(member.getMem_no());
//		model.addAttribute("friendList", friendList);
		return "redirect:/bud/budList.mo";
	}
	
	@RequestMapping("bud/budDelete")
	public String budDelete(int mem_no, HttpServletRequest request){
		HttpSession session = request.getSession();
		MemberDTO member = (MemberDTO)session.getAttribute("memberSession");
		int my_mem_no = member.getMem_no();

		FriendDTO friend = new FriendDTO();
		friend.setFr_member(mem_no);
		friend.setFr_friend(my_mem_no);
		service.deleteFriend(friend);

		friend.setFr_member(my_mem_no);
		friend.setFr_friend(mem_no);
		service.deleteFriend(friend);
		
		member = service.getMember(my_mem_no);
		session.setAttribute("memberSession", member);
		return "bud/budList";
	}
	
	@RequestMapping("bud/budDelComment")
	public String budDelComment(int cm_no){
		service.deleteCommentMember(cm_no);
		return "jsonView";
	}
	@RequestMapping("bud/budDelCommentShop")
	public String budDelCommentShop(int cs_no){
		service.deleteCommentShop(cs_no);
		return "jsonView";
	}
	
}
